package com.mogujie.tt.utils;

/**
 * Created by user on 2015/12/10.
 */

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.mogujie.tt.config.PersonalUserInfoConstant;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @ClassName: loginIn
 * @Description: 登录按钮验证
 * @author ch
 *
 */
public class LoginIn extends AsyncTask<String, Void, Void> {
    int rskey = 0;
    private String newMessageInfo;
    private Context context;
    public LoginIn (Context context){
        this.context =context;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(String... parm) {
        // 异步请求返回是否登录成功
        rskey = loginChecking();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        newMessageInfo = "";
        if (rskey == 1) {
        //    Toast.makeText(context,"登录成功！！！",Toast.LENGTH_LONG).show();
            Log.v("LoginIn", " PersonalUserInfoConstant.userid = " + PersonalUserInfoConstant.userid);
            Log.v("LoginIn"," PersonalUserInfoConstant.username = "+PersonalUserInfoConstant.username);
            Log.v("LoginIn"," PersonalUserInfoConstant.password = "+PersonalUserInfoConstant.password);
        }else if(rskey==-1){
            newMessageInfo="您还未配置云服务器地址，请先配置";
        }  else if (rskey == 3) {
            newMessageInfo = "登录失败，用户或密码错误。";
        }  else if(rskey==404){
            newMessageInfo=HttpUtilsApache.HTTP_404;
        }else if(rskey==500){
            newMessageInfo=HttpUtilsApache.HTTP_500;
        }else if(rskey==900){
            newMessageInfo=HttpUtilsApache.HTTP_900;
        }else if(rskey==901){
            newMessageInfo=HttpUtilsApache.HTTP_901;
        }else if(rskey==902){
            newMessageInfo=HttpUtilsApache.HTTP_902;
        }else if(rskey==903){
            newMessageInfo=HttpUtilsApache.HTTP_903;
        }  else {
            newMessageInfo = "登录失败，用户或密码错误。";
        }

        if(rskey!=1){
         //   Toast.makeText(context,newMessageInfo,Toast.LENGTH_LONG).show();
            PersonalUserInfoConstant.userid ="";
        }

    }

    /**
     * 检查是否登录成功解析json内容
     *
     * @return
     */
    public int loginChecking() {
        int rt = 0;
        HttpUtilsApache httpGetorPostUtil = new HttpUtilsApache();

        String userName = "hs@sxun.cn".toString().trim();
        String userPwd = "aaaaaa".toString().trim();
        Map<String, Object> params = new HashMap<String, Object>();
        //base64加密访问
        params.put("callback", "?");
        params.put("username", Base64.encode(userName.getBytes()));
        params.put("password", Base64.encode(userPwd.getBytes()));
        params.put("act", Base64.encode("ajaxlogin".getBytes()));
        params.put("time", System.currentTimeMillis());
        String ip="172.16.0.2:80";
        if(ip==null || "".equals(ip)){
            rt=-1;
        }else{

            //base64加密访问
            String url="http://"+ip+"/dashboard/site/act";
            String strjson="";

            strjson= httpGetorPostUtil.usHttpGet(url, params);

            JsonUtil jsonUtil = new JsonUtil();


            if (strjson != null && !"".equals(strjson)) {
                int rtjson=strjson.indexOf("?(");
                if(rtjson==0){
                    String getjsonstr=strjson.substring(2, strjson.lastIndexOf(")"));
                    try {

                        rt = Integer.valueOf(jsonUtil.getJsonObject(getjsonstr).getString("successflag"));
                        String cookie_userid=jsonUtil.getJsonObject(getjsonstr).getString("cookie_userid");


                        PersonalUserInfo userInfo=new PersonalUserInfo();
                        userInfo.setUserid(cookie_userid);
                        userInfo.setUsername(userName);
                        userInfo.setPassword(userPwd);
                        PersonalUserInfoConstant.userid =userInfo.getUserid();
                        PersonalUserInfoConstant.username =userInfo.getUsername();
                        PersonalUserInfoConstant.password =userInfo.getPassword();

                    } catch (JSONException e) {
                        rt=3;
                    }

                }else{
                    rt=Integer.valueOf( strjson);
                }
            }
        }
        return rt;
    }
}


